package com.weds.data_sync.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weds.data_sync.advice.exception.BizCodeException;
import com.weds.data_sync.constant.BizCodeEnum;
import com.weds.data_sync.mapper.DataSyncScriptRelationMapper;
import com.weds.data_sync.pojo.dto.PageDto;
import com.weds.data_sync.pojo.dto.ScriptGroupDto;
import com.weds.data_sync.pojo.entity.DataSyncScriptGroupDO;
import com.weds.data_sync.mapper.DataSyncScriptGroupMapper;
import com.weds.data_sync.pojo.entity.DataSyncScriptRelationDO;
import com.weds.data_sync.service.ScriptGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weds.data_sync.util.CommonUtil;
import com.weds.data_sync.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 脚本分组表 服务实现类
 * </p>
 *
 * @author lhw
 * @since 2023-12-29
 */
@Slf4j
@Service
public class ScriptGroupServiceImp extends ServiceImpl<DataSyncScriptGroupMapper, DataSyncScriptGroupDO> implements ScriptGroupService {

    @Autowired
    private DataSyncScriptGroupMapper groupMapper;

    @Autowired
    private DataSyncScriptRelationMapper scriptRelationMapper;

    @Override
    public Integer add(ScriptGroupDto scriptGroupDto) {
        DataSyncScriptGroupDO dataSyncScriptGroupDO = new DataSyncScriptGroupDO();
        BeanUtils.copyProperties(scriptGroupDto, dataSyncScriptGroupDO);
        return groupMapper.insert(dataSyncScriptGroupDO);
    }

    @Override
    public Integer del(List<Long> ids) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            throw new BizCodeException(BizCodeEnum.SCRIPT_DEL_NOCHOOSE);
        }
        // 判断分组下有脚本禁止删除
        Long scriptCount = scriptRelationMapper.selectCount(new QueryWrapper<DataSyncScriptRelationDO>()
                .in("group_id", ids)
                .eq("use_status_id", 1)
                .eq("is_delete", 0));
        if (scriptCount > 0) {
            throw new BizCodeException(BizCodeEnum.GROUP_DEL_FAIL);
        }

        return groupMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer update(ScriptGroupDto scriptGroupDto) {
        Long id = scriptGroupDto.getId();
        if (Objects.isNull(id)) {
            return 0;
        }
        DataSyncScriptGroupDO groupDO = new DataSyncScriptGroupDO();
        BeanUtils.copyProperties(scriptGroupDto, groupDO);
        return groupMapper.updateById(groupDO);
    }

    @Override
    public JsonData.PageData<DataSyncScriptGroupDO> queryData(PageDto pageDto) {
        Page<DataSyncScriptGroupDO> pageInfo = new Page<>(pageDto.getPage(), pageDto.getSize());
        groupMapper.selectPage(pageInfo, new QueryWrapper<DataSyncScriptGroupDO>()
                .orderByAsc("id"));
        return CommonUtil.pageToDto(pageInfo);
    }
}
